package com.kkd.kkdapp.adapter;

import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.OverdueRepaymentActivity;
import com.kkd.kkdapp.activity.PaymentActivity;
import com.kkd.kkdapp.http.entity.PaingCreditDetailList;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PaingCreditDetailAdapter extends BaseAdapter {

	private Context context;
	private List<PaingCreditDetailList> mList;

	// private String bill_id = "";

	public PaingCreditDetailAdapter(Context context,
			List<PaingCreditDetailList> mList) {
		this.context = context;
		this.mList = mList;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder;
		if (convertView == null) {
			convertView = View.inflate(context,
					R.layout.paingcreditdetail_item, null);
			mViewHolder = new ViewHolder();
			convertView.setTag(mViewHolder);
			mViewHolder.repayment = (TextView) convertView
					.findViewById(R.id.repayment);
			mViewHolder.expect = (TextView) convertView
					.findViewById(R.id.expect_tv);
			mViewHolder.shouldpaymoney = (TextView) convertView
					.findViewById(R.id.shouldpaymoney);
			mViewHolder.bill_tv = (TextView) convertView
					.findViewById(R.id.bill);
			mViewHolder.date_tv = (TextView) convertView
					.findViewById(R.id.date_tv);
			mViewHolder.desc_tv = (TextView) convertView
					.findViewById(R.id.desc_tv);
			mViewHolder.amortizedloans = (TextView) convertView
					.findViewById(R.id.amortizedloans);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}

		// 期数
		mViewHolder.expect.setTag(position);
		mViewHolder.expect.setText("第" + mList.get(position).getBill_num()
				+ "期");
		// 还款状态：还款，逾期还款，未到期
		mViewHolder.repayment.setTag(position);
		mViewHolder.repayment.setText(mList.get(position).getButton_text());
		mViewHolder.amortizedloans.setVisibility(View.GONE);
		if (!"未到期".equals(mList.get(position).getButton_text())) {
			mViewHolder.amortizedloans.setVisibility(View.VISIBLE);
			mViewHolder.amortizedloans.setText("已还："
					+ mList.get(position).getHavePayMoney() + "元");
			// if ("已还清".equals(mList.get(position).getButton_text())) {
			// mViewHolder.amortizedloans.setTextColor(0xffa8a8a8);
			// } else {
			// mViewHolder.amortizedloans.setTextColor(0xff000000);
			// }
		}
		// button状态码，1：可以点击 0：不可以点击
		int status = mList.get(position).getStatus();

		final int yuqi_status = mList.get(position).getYuqi_status();
		mViewHolder.repayment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String bill_id = mList.get(position).getBill_id();
				if (yuqi_status == 1) {
					Intent intent = new Intent(context,
							OverdueRepaymentActivity.class);
					intent.putExtra("bill_id", bill_id);
					context.startActivity(intent);
				} else if (yuqi_status == 0) {
					Intent intent = new Intent(context, PaymentActivity.class);
					intent.putExtra("bill_id", bill_id);
					context.startActivity(intent);
				}
			}
		});
		if (status == 1) {
			mViewHolder.repayment.setEnabled(true);
			mViewHolder.repayment.setClickable(true);
		} else if (status == 0) {
			mViewHolder.repayment.setClickable(false);
			mViewHolder.repayment.setEnabled(false);
		}
		// 应还金额
		mViewHolder.shouldpaymoney.setTag(position);
		mViewHolder.shouldpaymoney.setText("应还金额："
				+ mList.get(position).getShouldPayMoney() + "元");
		// 应还金额描述
		mViewHolder.bill_tv.setTag(position);
		mViewHolder.bill_tv.setText(ToDBC(mList.get(position).getBill_text()));
		// 还款日期
		mViewHolder.date_tv.setTag(position);
		mViewHolder.date_tv.setText("还款日期："
				+ mList.get(position).getShouldPayDate());
		// 到期天数
		mViewHolder.desc_tv.setTag(position);
		mViewHolder.desc_tv.setText(mList.get(position).getDesc());
		// 字体颜色状态码：1：黑色 0：灰色
		int text_status = mList.get(position).getText_status();
		if (text_status == 1) {
			mViewHolder.amortizedloans.setTextColor(context.getResources()
					.getColor(R.color.gray_999999));
			mViewHolder.expect.setTextColor(context.getResources().getColor(
					R.color.black_333333));
			mViewHolder.shouldpaymoney.setTextColor(context.getResources()
					.getColor(R.color.black_333333));
			mViewHolder.date_tv.setTextColor(context.getResources().getColor(
					R.color.black_333333));
			mViewHolder.desc_tv.setTextColor(context.getResources().getColor(
					R.color.black_333333));
		} else if (text_status == 0) {
			mViewHolder.amortizedloans.setTextColor(context.getResources()
					.getColor(R.color.gray_a8a8a8));
			mViewHolder.expect.setTextColor(context.getResources().getColor(
					R.color.gray_a8a8a8));
			mViewHolder.shouldpaymoney.setTextColor(context.getResources()
					.getColor(R.color.gray_a8a8a8));
			mViewHolder.date_tv.setTextColor(context.getResources().getColor(
					R.color.gray_a8a8a8));
			mViewHolder.desc_tv.setTextColor(context.getResources().getColor(
					R.color.gray_a8a8a8));
		}
		return convertView;
	}

	class ViewHolder {
		TextView repayment;
		TextView expect;
		TextView shouldpaymoney;
		TextView bill_tv;
		TextView date_tv;
		TextView desc_tv;
		TextView amortizedloans;
	}

	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}

}
